package gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import server.TodoItem;

public class ModifyInfoDialog extends JDialog implements ActionListener{
    private JButton okButton = null;
    private JComboBox priority;
    private JButton cancelButton = null;
    private JTextField deadlineText;
    private JTextArea content;
    private TodoItem todoAnswer = null;
    private TodoItem todo;
//    public boolean getAnswer() { return answer; }

    public ModifyInfoDialog(TodoItem todo, JFrame frame, boolean modal) {
        super(frame, modal);
        setSize(300, 280);
        setResizable(false);
        setTitle("Modify information");
        
        this.todo = todo;

        JPanel panel = new JPanel();
        panel.setLayout(null);
        getContentPane().add(panel);
        JLabel priorityLabel = new JLabel("Priority");
        priorityLabel.setSize(80, 25);
        priorityLabel.setLocation(20, 20);
        panel.add(priorityLabel);
        
        priority = new JComboBox(new Integer[]{1,2,3});
        priority.setSize(60,30);
        priority.setLocation(90, 20);
        priority.setSelectedItem(todo.getPriority());
        panel.add(priority);
        
        JLabel deadlineLabel = new JLabel("Deadline");
        deadlineLabel.setSize(80, 25);
        deadlineLabel.setLocation(20,50);
        panel.add(deadlineLabel);
        deadlineText = new JTextField(todo.getDeadline());
        deadlineText.setColumns(15);
        deadlineText.setSize(110,30);
        deadlineText.setLocation(90,50);
        panel.add(deadlineText);
        
        content = new JTextArea(todo.getContent(),10,20);
        JScrollPane scrollableContent = new JScrollPane(
				content, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollableContent.setSize(265,100);
        scrollableContent.setLocation(20, 90);
        panel.add(scrollableContent);
        
        okButton = new JButton("OK");
        okButton.addActionListener(this);
        okButton.setSize(60,25);
        okButton.setLocation(20,200);
        panel.add(okButton);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        cancelButton.setSize(90, 25);
        cancelButton.setLocation(70, 200);
        panel.add(cancelButton);  
        setLocationRelativeTo(frame);
        
    }
    
    public ModifyInfoDialog(JFrame frame, boolean modal){
        this(new TodoItem(-1, 1, "", "", 1, ""),frame,modal);
        
//        ModifyInfoDialog(todo,frame,modal);
        
    }
    
    public TodoItem showDialog(){
        setVisible(true);
        return todoAnswer;
    }

    public void actionPerformed(ActionEvent e) {
        if(okButton == e.getSource()) {
            todoAnswer = new TodoItem();
            todoAnswer.setId(todo.getId());
            todoAnswer.setVersion(todo.getVersion());
            todoAnswer.setContent(content.getText());
            todoAnswer.setDeadline(deadlineText.getText());
            Date today = new Date();
            Format format = new SimpleDateFormat("E, dd-MMM-yyyy HH:mm:ss");
            todoAnswer.setLasUpdate(format.format(today));
//            System.out.println(Calendar.getInstance().getTime().toString());
            todoAnswer.setPriority((Integer) priority.getSelectedItem());
            setVisible(false);
        }
        else if(cancelButton == e.getSource()) {
            todoAnswer = null;
            setVisible(false);
        }
    }

}
